﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Module_PhotoController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index(string actionId)
        {
            this.ValidateRequest = false;
            ViewData["id"] = actionId;
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回图片列表
        /// </summary>
        /// <returns>json</returns>
        public string List()
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Module_PhotoEntity> Module_PhotoSv = ServiceBuilder.BuildModule_PhotoService();
            List<Module_PhotoEntity> module_photoList = Module_PhotoSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(module_photoList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看图片 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Module_PhotoEntity> Module_PhotoSv = ServiceBuilder.BuildModule_PhotoService();
            int intId=Utils.StringToInt(actionId,0);
            Module_PhotoEntity module_photo = Module_PhotoSv.Look(intId);
            if (module_photo != null)
            {
                IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                jsDataConvert.DateTimeFormat = "yyyy-MM-dd hh:mm:ss";
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(module_photo,Formatting.Indented, jsDataConvert), "}");
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Module_PhotoEntity module_photo = new Module_PhotoEntity();
                module_photo.DataTable_Action = DataTableAction.Update;
                module_photo.Id = intId;
                module_photo.ChannelId = (Int32)Utils.GetQueryAndFormString("ChannelId", this, 10,  1 , DataType.Int);
                module_photo.Path = (string)Utils.GetQueryAndFormString("Path", this, 10, 1, DataType.CharAndNum);
                module_photo.Title = (String)Utils.GetQueryAndFormString("Title", this, 150,  1 , DataType.Str);
                module_photo.TColor = (String)Utils.GetQueryAndFormString("TColor", this, 50,  0, DataType.Str);
                module_photo.AddDate = (DateTime)Utils.GetQueryAndFormString("AddDate", this, 23,  1 , DataType.Dat);
                module_photo.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 2147483647,  1 , DataType.Str);
                module_photo.Editor = (String)Utils.GetQueryAndFormString("Editor", this, 20,  1 , DataType.Str);
                module_photo.Author = (String)Utils.GetQueryAndFormString("Author", this, 30,  0, DataType.Str);
                module_photo.Tags = (String)Utils.GetQueryAndFormString("Tags", this, 60,  0, DataType.Str);
                module_photo.ViewNum = (Int32)Utils.GetQueryAndFormString("ViewNum", this, 10,  1 , DataType.Int);
                module_photo.IsPass = (Int32)Utils.GetQueryAndFormString("IsPass", this, 2, 1, DataType.Int);
                module_photo.IsImg =  (bool)Utils.GetQueryAndFormString("IsPass", this, 6, 1, DataType.Bool);
                module_photo.Img = (String)Utils.GetQueryAndFormString("Img", this, 150,  1 , DataType.Str);
                module_photo.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  1 , DataType.Int);
                module_photo.UserId = (Int32)Utils.GetQueryAndFormString("UserId", this, 10,  1 , DataType.Int);
                module_photo.ReadGroup = (Int32)Utils.GetQueryAndFormString("ReadGroup", this, 10,  1 , DataType.Int);
                module_photo.SourceFrom = (String)Utils.GetQueryAndFormString("SourceFrom", this, 30,  1 , DataType.Str);
                module_photo.PhotoUrl = (String)Utils.GetQueryAndFormString("PhotoUrl", this, 2147483647,  1 , DataType.Str);
                module_photo.PageSize = (Int32)Utils.GetQueryAndFormString("PageSize", this, 10,  1 , DataType.Int);
                module_photo.ReadComment = (Int32)Utils.GetQueryAndFormString("ReadComment", this, 10,  0, DataType.Int);
                module_photo.WriteComment = (Int32)Utils.GetQueryAndFormString("WriteComment", this, 10,  0, DataType.Int);
                module_photo.SupportNum = (Int32)Utils.GetQueryAndFormString("SupportNum", this, 10,  1 , DataType.Int);
                module_photo.AgainstNum = (Int32)Utils.GetQueryAndFormString("AgainstNum", this, 10,  1 , DataType.Int);
                module_photo.Enabled = (bool)Utils.GetQueryAndFormString("Enabled", this, 1,  1 , DataType.Bool);
                IDataService<Module_PhotoEntity> Module_PhotoSv = ServiceBuilder.BuildModule_PhotoService();
                if (Module_PhotoSv.Update(module_photo) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增图片
        /// </summary>
        /// <returns>json</returns>
        public string Insert(string actionId)
        {

            int channelId = 0;
            channelId = Utils.StringToInt(actionId.ToString(), 0);
            if (channelId != 0)
            {
                IDataService<Normal_ChannelEntity> channelSv = ServiceBuilder.BuildNormal_ChannelService();
                Normal_ChannelEntity channel = channelSv.Look(channelId);
                if (channel != null)
                {
                    Module_PhotoEntity module_photo = new Module_PhotoEntity();
                    module_photo.DataTable_Action = DataTableAction.Insert;
                    module_photo.ChannelId = channel.Id;
                    module_photo.Path = channel.Path;
                    module_photo.Title = (String)Utils.GetQueryAndFormString("Title", this, 150, 1, DataType.Str);
                    module_photo.TColor = (String)Utils.GetQueryAndFormString("TColor", this, 50, 0, DataType.Str);
                    module_photo.AddDate = (DateTime)Utils.GetQueryAndFormString("AddDate", this, 23, 1, DataType.Dat);
                    module_photo.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 2147483647, 1, DataType.Str);
                    module_photo.Editor = (String)Utils.GetQueryAndFormString("Editor", this, 20, 1, DataType.Str);
                    module_photo.Author = (String)Utils.GetQueryAndFormString("Author", this, 30, 0, DataType.Str);
                    module_photo.Tags = (String)Utils.GetQueryAndFormString("Tags", this, 60, 0, DataType.Str);
                    module_photo.ViewNum = (Int32)Utils.GetQueryAndFormString("ViewNum", this, 10, 1, DataType.Int);
                    module_photo.IsPass = (Int32)Utils.GetQueryAndFormString("IsPass", this, 2, 1, DataType.Int);
                    module_photo.IsImg = (bool)Utils.GetQueryAndFormString("IsImg", this, 1, 1, DataType.Bool);
                    module_photo.Img = (String)Utils.GetQueryAndFormString("Img", this, 150, 1, DataType.Str);
                    module_photo.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10, 1, DataType.Int);
                    module_photo.UserId = (Int32)Utils.GetQueryAndFormString("UserId", this, 10, 1, DataType.Int);
                    module_photo.ReadGroup = (Int32)Utils.GetQueryAndFormString("ReadGroup", this, 10, 1, DataType.Int);
                    module_photo.SourceFrom = (String)Utils.GetQueryAndFormString("SourceFrom", this, 30, 1, DataType.Str);
                    module_photo.PhotoUrl = (String)Utils.GetQueryAndFormString("PhotoUrl", this, 2147483647, 1, DataType.Str);
                    module_photo.PageSize = (Int32)Utils.GetQueryAndFormString("PageSize", this, 10, 1, DataType.Int);
                    module_photo.ReadComment = (Int32)Utils.GetQueryAndFormString("ReadComment", this, 10, 0, DataType.Int);
                    module_photo.WriteComment = (Int32)Utils.GetQueryAndFormString("WriteComment", this, 10, 0, DataType.Int);
                    module_photo.SupportNum = (Int32)Utils.GetQueryAndFormString("SupportNum", this, 10, 1, DataType.Int);
                    module_photo.AgainstNum = (Int32)Utils.GetQueryAndFormString("AgainstNum", this, 10, 1, DataType.Int);
                    module_photo.Enabled = (bool)Utils.GetQueryAndFormString("Enabled", this, 1, 1, DataType.Bool);
                    IDataService<Module_PhotoEntity> Module_PhotoSv = ServiceBuilder.BuildModule_PhotoService();
                    if (Module_PhotoSv.Insert(module_photo) == "")
                    {
                        returnJson = "{success:'true'}";
                    }
                    else
                        returnJson = "{success:'false'}";
                    
                }
                else
                {
                    returnJson = "{success:'false',message:'栏目id错误'}";
                }

            }
            else
            {
                returnJson = "{success:'false',message:'栏目id错误'}";
            }
            return returnJson;

           
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Module_PhotoEntity module_photo = new Module_PhotoEntity();
                module_photo.Id = intId;
                IDataService<Module_PhotoEntity> Module_PhotoSv = ServiceBuilder.BuildModule_PhotoService();
                if (Module_PhotoSv.Delete(module_photo) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除图片
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Module_PhotoEntity> Module_PhotoSv = ServiceBuilder.BuildModule_PhotoService();
                if (Module_PhotoSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
